package com.jams.goodox.activity.serve;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.entity.resp.PartyWorkResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * chenjian
 * 党员服务,***热点
 */
public class PartyServiceActivity extends BaseActivity {

    @Bind(R.id.tabs)
    TabLayout mTabLayout;
    @Bind(R.id.mPager)
    ViewPager mPager;
    @Bind(R.id.lv_load)
    LoadView emptyView;
    /**
     * 小标题内容
     */
    private List<String> tittleList = new ArrayList<>();
    /**
     * 显示的Fragment 集合
     */
    private List<Fragment> contentList = new ArrayList<>();
    public final static String PARTY_SERVICE = "PARTY_SERVICE";
    public final static String TOWN_NEWS = "TOWN_NEWS";
    public final static String TITTLE_NEWS = "TITTLE_NEWS";
    private FragmentPagerAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_party_member_service;
    }

    @Override
    protected void init() {
        String tittle = StringUtils.newString(getIntent().getStringExtra(TITTLE_NEWS));
        initView();
        if (tittle.equals(PARTY_SERVICE)) {
            StyleUtil.titleBackKey(this, getString(R.string.party_member_service));
            getData("0");
        } else if (tittle.equals(TOWN_NEWS)) {
            StyleUtil.titleBackKey(this, getString(R.string.town_news));
            getData("1");
        }


    }

    /**
     *  党务信息,***热点信息
     * @param type "0":党务信息,"1":***热点
     */
    private void getData(final  String type) {

        EnterpriseService service = createCallService(EnterpriseService.class);
        Call<PartyWorkResp> call;
        switch (type){
            case "0":
                call = service.getPartyWorkData();
                break;
            case "1":
                call = service.getTownNewsData();
                break;
            default:
                emptyView.setVisibility(View.VISIBLE);
                return;
        }
        showLoading();
        if (call == null) {
            emptyView.setVisibility(View.VISIBLE);
            return;
        }
        enqueue(call, new RetrofitCallbackListener<PartyWorkResp>() {
            @Override
            public void onSuccess(Call<PartyWorkResp> call, PartyWorkResp result) {
                misLoading();
                try {
                    if (!CollectionUtils.hasData(result.data)) {
                        emptyView.setVisibility(View.VISIBLE);
                        return;
                    }
                    tittleList.clear();
                    contentList.clear();
                    for (PartyWorkResp.PartyWorkData data : result.data) {
                        tittleList.add(StringUtils.newString(data.name));
                        contentList.add(PartyServiceFragment.newInstance(StringUtils.newString(data.id),type));
                    }
                    if (tittleList != null && contentList != null) {
                        adapter.notifyDataSetChanged();
                        mTabLayout.setVisibility(View.VISIBLE);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
                emptyView.setVisibility(View.VISIBLE);
            }
        });
    }
    private void initView(){
        if (tittleList != null && contentList != null) {
            adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), tittleList, contentList);
            mPager.setAdapter(adapter);
            mTabLayout.setupWithViewPager(mPager);
            emptyView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
        }
    }



}
